package lion.web.interceptors;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lion.framework.core.conf.Config;
import lion.framework.core.conf.ConfigManager;
import lion.framework.core.web.request.RequestHandler;
import lion.framework.core.web.request.RequestInterceptor;

/**
 * 配置除了WEB-INF以外的其他受保护的目录,配置framework.xml
 *
 * <pre>
 * <protectedpath>
 * 		<path>/protected</path>
 * </protectedpath>
 *
 * time   2013 2013-4-24 上午11:30:30
 * mail   hl_0074@sina.com
 * </pre>
 *
 * @author lion.dev
 */
public class ProtectedPathInterceptor implements RequestInterceptor {

	private List<String> protectedPaths = new ArrayList<>();;

	public ProtectedPathInterceptor() {

		Config config = ConfigManager.getConfig("framework");
		protectedPaths = config.getList("framework.web.protectedpath.path");
	}

	@Override
	public void intercept(HttpServletRequest request, HttpServletResponse response, RequestHandler rh) throws Exception {

		String path = rh.getPath();

		if (protectedPaths != null && !protectedPaths.isEmpty()) {
			for (String p : protectedPaths) {
				if (p.equalsIgnoreCase(path) || path.matches(p)) { return; }
			}
		}

		rh.handle(request, response);
	}
}
